// Generated using webpack-cli https://github.com/webpack/webpack-cli
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')

const isProduction = process.env.NODE_ENV === 'production'
const stylesHandler = isProduction ? MiniCssExtractPlugin.loader : 'style-loader'

const config = {
  entry: {
    'background': './src/background.scripts/index.ts',
    'content.scripts': './src/content.scripts/index.ts',
    'popup': './src/popup.page/App.tsx'
  },
  output: {
    path: path.resolve(__dirname, 'dist')
  },
  devServer: {
    open: true,
    host: 'localhost'
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/popup.page/popup.html',
      chunks: [ 'popup' ]
    })
  ],
  module: {
    rules: [
      {
        test: /\.(js|ts)x?$/i,
        loader: 'babel-loader',
        exclude: [ '/node_modules/' ]
      },
      {
        test: /\.less$/i,
        use: [ stylesHandler, 'css-loader', 'postcss-loader', 'less-loader' ]
      },
      {
        test: /\.css$/i,
        use: [ stylesHandler, 'css-loader', 'postcss-loader' ]
      },
      {
        test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
        type: 'asset'
      }

      // Add your rules for custom modules here
      // Learn more about loaders from https://webpack.js.org/loaders/
    ]
  },
  resolve: {
    extensions: [ '.tsx', '.ts', '.js', '.jsx' ]
  }
}

module.exports = () => {
  if (isProduction) {
    config.mode = 'production'

    config.plugins.push(new MiniCssExtractPlugin())

  } else {
    config.mode = 'development'
  }
  return config
}
